package com.joysuch.wwyt.attendant.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.attendant.bean.AttendantPlanBean;
import com.joysuch.wwyt.attendant.bean.AttendantPlanDetailBean;
import com.joysuch.wwyt.attendant.bean.AttendantPlanReqBean;
import com.joysuch.wwyt.attendant.service.AttendantPlanService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 跟班计划
 *
 * @author xsx
 * @version 1.0
 * @date 2023/2/15 13:53
 */
@RestController
@RequestMapping("/attendant/plan")
@Api(tags = "跟班计划")
@Slf4j
public class AttendantPlanController {

    @Autowired
    private AttendantPlanService attendantPlanService;


    @PostMapping("save")
    @ApiOperation("保存跟班计划")
    @RequiresPermissions("attendant:plan:save")
    public ResultBean save(@RequestBody AttendantPlanBean bean) throws ApiBusinessException {
        attendantPlanService.saveAttendantPlan(bean);
        return ResultBean.defaultSuccessResult();
    }


    @PostMapping("update")
    @ApiOperation("修改跟班计划")
    @RequiresPermissions("attendant:plan:update")
    public ResultBean update(@RequestBody AttendantPlanBean bean) throws ApiBusinessException {
        attendantPlanService.updateAttendantPlan(bean);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("findById")
    @ApiOperation("查看跟班计划")
    public ResultBean findById(@RequestParam(value = "id") Long id) {
        AttendantPlanBean bean = attendantPlanService.findAttendantPlanBean(id);
        return ResultBean.success(bean);
    }

    @GetMapping("selectPage")
    @ApiOperation("查询跟班计划列表")
    public ResultBean selectPage(AttendantPlanReqBean reqBean) {
        Page<AttendantPlanDetailBean> result = attendantPlanService.selectPage(reqBean);
        return ResultBean.pageData(result.getRecords(), result.getTotal());
    }


    @PostMapping("delete")
    @ApiOperation("删除跟班计划")
    @RequiresPermissions("attendant:plan:delete")
    public ResultBean delete(Long id) {
        attendantPlanService.delete(id);
        return ResultBean.defaultSuccessResult();
    }


    @PostMapping("deleteByIds")
    @ApiOperation("批量删除跟班计划")
    @RequiresPermissions("attendant:plan:deleteByIds")
    public ResultBean deleteByIds(@RequestBody List<Long> ids) {
        attendantPlanService.deleteByIds(ids);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("importExcelData")
    @ApiOperation("跟班计划导入 way 1新增 2覆盖 3修改")
    @RequiresPermissions("attendant:plan:import")
    public ResultBean importExcelData(Integer way, MultipartFile file) {
        if (!file.getOriginalFilename().endsWith("xlsx") && !file.getOriginalFilename().endsWith("xls")) {
            return ResultBean.success(Lists.newArrayList("文件类型不正确"));
        }
        try {
            List<String> list = attendantPlanService.importExcelData(way, file);
            return ResultBean.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.fail(101, "导入失败");
        }
    }

    @GetMapping("export")
    @ApiOperation("批量导出")
    @RequiresPermissions("attendant:plan:export")
    public ResultBean export(String ids, HttpServletResponse response) {

        return attendantPlanService.export(ids,response);
    }

}
